package
{
	import org.flixel.*; //Get access to all the wonders flixel has to offer

	
	public class PlayCvInvaderState extends FlxState		//The class declaration for the main game state
	{
		protected var _dudeType:int = 1;
		protected var _dude:Dude;				
		protected var _dudeCvs:FlxGroup;	
		protected var _snoop:Snoop;
		protected var _enemies:FlxGroup;	
		protected var _skills:FlxGroup;
		protected var _score:FlxText;
		protected var skillTitle:Boolean = false;
		protected var qSkills:Array; 
		protected var skillsCounters:Array;
		
		public function PlayCvInvaderState(t:int){
			super();
			_dudeType = t;
		}
		
		override public function create():void
		{
			var i:int;
			
			//Backgrounds
			var b0:FlxSprite = new FlxSprite(0,0);
			b0.loadGraphic(Resources.Back1,false);
			add(b0);
			
			//Panel
			var t0:FlxText = new FlxText(5, 3,50,"Score:");
			t0.size = 10;
			add(t0);
			_score = new FlxText(5, 15,FlxG.width,FlxG.score.toString());
			_score.size = 15;
			add(_score);
			
			
			
			//Dude
			var s:FlxSprite;
			_dudeCvs = new FlxGroup();
			for(i = 0; i < 100; i++)			
			{
				s = new Cvs(100,1000);	//Ojo con y	< 100		
				_dudeCvs.add(s);			
			}
			add(_dudeCvs);

			_dude = new Dude(_dudeType,_dudeCvs.members);
			add(_dude);

			//Skills
			var k:Skill;
			_skills = new FlxGroup();
			for(i = 0; i < 7; i++) 
			{
				k = new Skill(-100,-100,i % 2);
				_skills.add(k);
			}
			add(_skills);
			qSkills = new Array(_skills.members.length);
			
			//Snoop
			 _snoop = new Snoop(_skills.members);
			 skillsCounters = new Array(_skills.members.length);
			add(_snoop);
			
			
			//Enemies
			var a:Enemy;
			_enemies = new FlxGroup();
			var yOffset:int;
			for(i = 0; i < 5; i++)
			{
				yOffset = 156 + (44 * i);
				var q:int =  (FlxU.random()*10 / 2);
				if(q < 1) 
				{ 
					q = 1;
				}
				for(var j:int = 0; j < q ; j++) { 
					a = new Enemy(yOffset, (i % 5) + 1,( i % 2) == 1 );
					_enemies.add(a);
				}
				
			}
			add(_enemies);
			
			
			
		}
		
		override public function update():void
		{
			FlxU.overlap(_enemies,_dudeCvs,cvTrapped);
			FlxU.overlap(_snoop,_dudeCvs,cvNailed);
			FlxU.overlap(_skills,_dude,skillNailed);
			
			_score.text = FlxG.score.toString();
			
			super.update();
			
			if(_dudeCvs.getFirstAlive() == null				
				||_enemies.getFirstExtant() == null)
			{
				FlxG.state = new PlayCvInvaderState(_dudeType);	
				return;
			}
		}
		
		protected function cvTrapped(Object1:FlxObject,Object2:FlxObject):void
		{
			Object1.kill()
			if(Object2 is Cvs){
				FlxG.score -=900;
				Object2.kill();	
			}
		}
		
		protected function cvNailed(Object1:FlxObject,Object2:FlxObject):void
		{
			//Object1.kill()
			if(Object2 is Cvs){
				FlxG.score +=100;
				Object2.kill();	
			}
		}


		
		
		protected function skillNailed(Object1:FlxObject,Object2:FlxObject):void 
		{
			FlxG.score +=500;
	
			if(Object1 is Skill) {
				var s:Skill = Skill(Object1);
				var skt:int = s.getSkillType();
				
				if(qSkills[skt] == null || qSkills[skt] == 0){
					var tt:Class = s.getSkillTypeClass(skt);
					var sp:FlxSprite = new FlxSprite(FlxG.width - 100, 26 + (22 * skt) );
					sp.loadGraphic(tt);
					add(sp);
					qSkills[skt] = 1;
				} else {
					qSkills[skt] += 1;
				}
				var st:FlxText = null;
				if(qSkills[skt] == 2){
					st = new FlxText( FlxG.width - 135, 26 + (22* skt),100, qSkills[skt].toString() + " x ");
					st.size = 12;
					add(st);
					skillsCounters[skt] = st;
				} 
				if(qSkills[skt] > 2){
					st = skillsCounters[skt];
					st.text = qSkills[skt].toString() + " x ";
				}
				
				if(!skillTitle) {
					var st1:FlxText = new FlxText( FlxG.width - 104, 8,100,"Skills");
					st1.size = 12;
					add(st1);
					skillTitle = false;
				}
			}
			Object1.kill();
		}

		
	}
}
